package edu.hawaii.kukui

class HomeController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        def cOne = Home.createCriteria()
      def columnOne = cOne {
        eq("column", 1)
        order("row", "asc")
      }

      def cTwo = Home.createCriteria()
      def columnTwo = cTwo {
        eq("column", 2)
        order("row", "asc")
      }

      def cThree = Home.createCriteria()
      def columnThree = cThree {
        eq("column", 3)
        order("row", "asc")
      }
      return [columnOne: columnOne, columnTwo: columnTwo, columnThree: columnThree]
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [homeInstanceList: Home.list(params), homeInstanceTotal: Home.count()]
    }

    def create = {
        def homeInstance = new Home()
        homeInstance.properties = params
        return [homeInstance: homeInstance]
    }

    def save = {
        def homeInstance = new Home(params)
        if (homeInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'home.label', default: 'Home'), homeInstance.id])}"
            redirect(action: "show", id: homeInstance.id)
        }
        else {
            render(view: "create", model: [homeInstance: homeInstance])
        }
    }

    def show = {
        def homeInstance = Home.get(params.id)
        if (!homeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'home.label', default: 'Home'), params.id])}"
            redirect(action: "list")
        }
        else {
            [homeInstance: homeInstance]
        }
    }

    def edit = {
        def homeInstance = Home.get(params.id)
        if (!homeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'home.label', default: 'Home'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [homeInstance: homeInstance]
        }
    }

    def update = {
        def homeInstance = Home.get(params.id)
        if (homeInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (homeInstance.version > version) {
                    
                    homeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'home.label', default: 'Home')] as Object[], "Another user has updated this Home while you were editing")
                    render(view: "edit", model: [homeInstance: homeInstance])
                    return
                }
            }
            homeInstance.properties = params
            if (!homeInstance.hasErrors() && homeInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'home.label', default: 'Home'), homeInstance.id])}"
                redirect(action: "show", id: homeInstance.id)
            }
            else {
                render(view: "edit", model: [homeInstance: homeInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'home.label', default: 'Home'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def homeInstance = Home.get(params.id)
        if (homeInstance) {
            try {
                homeInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'home.label', default: 'Home'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'home.label', default: 'Home'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'home.label', default: 'Home'), params.id])}"
            redirect(action: "list")
        }
    }
}
